package com.zhu.storesmanager.views;

import java.util.ArrayList;
import java.util.HashMap;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.SpecialTaskActivity;
import com.zhu.storesmanager.SvSelectVisitContentActivity;
import com.zhu.storesmanager.SvStoreBoothActivity;
import com.zhu.storesmanager.SvStoreCompeteActivity;
import com.zhu.storesmanager.SvStorePOSMActivity;
import com.zhu.storesmanager.SvStorePromoterInfoActivity;
import com.zhu.storesmanager.SvStoreSalesActivity;
import com.zhu.storesmanager.SvStoreSampleActivity;
import com.zhu.storesmanager.models.VisitTaskInfo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

/**
 * 弹出POP菜单
 */
public class PopMenu{

	public static final int MENU_SAMPLE = 0; //门店出样
	public static final int MENU_BOOTH = 1; //门店展台
	public static final int MENU_SALES = 2; //门店销售
	public static final int MENU_POSM = 3; //POSM
	public static final int MENU_COMPETE = 4; //竞品信息
	public static final int MENU_PROMOTER = 5; //促销员信息
	public static final int MENU_TASK = 6; //特殊任务

	private PopupWindow mMenuWindow = null; //弹窗 
	private Activity mActivity = null;
	private int mCurIndex = -1; //当前所在界面索引
	private VisitTaskInfo mVisitTaskInfo = null; //拜访门店数据

	int[] menu_image_array = { R.drawable.menu_sample,
            R.drawable.menu_booth, R.drawable.menu_sales,
            R.drawable.menu_posm, R.drawable.meun_compete,
            R.drawable.menu_pomoter, R.drawable.menu_task};
	private GridView mGridView = null; //弹出内容
	private View mLocationView = null; //显示在view

	/**
	 * @param context 上下文
	 * @param curIndex 当前所在界面索引
	 * @param locationView pop显示位置
	 */
	public PopMenu(Activity activity, View locationView, int curIndex) {
		mLocationView = locationView;
		mCurIndex = curIndex;
		mActivity = activity;
		mGridView = (GridView)LayoutInflater.from(mActivity).inflate(R.layout.popup_menu, null);
		mGridView.setAdapter(getMenuAdapter(mActivity.getResources().getStringArray(R.array.visit_content_menu), menu_image_array));
		mGridView.setOnItemClickListener(new OnItemClickListener() { //点击监听
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
            	mMenuWindow.dismiss();
            	gotoActivity(arg2);
            }
        });
		//创建PopupWindow
		mMenuWindow = new PopupWindow(mGridView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		mMenuWindow.setFocusable(true);
		mMenuWindow.setOutsideTouchable(true);
		mMenuWindow.update();
		mMenuWindow.setBackgroundDrawable(new BitmapDrawable());
	}

	/**
	 *设置拜访数据 
	 */
	public void setVisitTaskInfo(VisitTaskInfo visitTaskInfo){
		mVisitTaskInfo = visitTaskInfo;
	}

	/**
	 * 显示Pop菜单
	 */
	public void showMenu(){
		mMenuWindow.showAtLocation(mLocationView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, mLocationView.getHeight());
	}

	/**
	  * 构造菜单Adapter
	  * 
	  * @param menuNameArray
	  *            名称
	  * @param imageResourceArray
	  *            图片
	  * @return SimpleAdapter
	  */
	 private SimpleAdapter getMenuAdapter(String[] menuNameArray,
	   int[] imageResourceArray) {
	   ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
	   for (int i = 0; i < menuNameArray.length; i++) {
	        HashMap<String, Object> map = new HashMap<String, Object>();
	        map.put("itemImage", imageResourceArray[i]);
	        map.put("itemText", menuNameArray[i]);
	        data.add(map);
	   }
	   SimpleAdapter simperAdapter = new SimpleAdapter(mActivity, data,
	        R.layout.item_menu, new String[] { "itemImage", "itemText" },
	        new int[] { R.id.item_image, R.id.item_text });
	        return simperAdapter;
	  }

	private void gotoActivity(int index) {
		if(mCurIndex == index){
			return;
		}
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putParcelable("info", mVisitTaskInfo);
		intent.putExtras(bundle);
		switch (index) {
		case MENU_SAMPLE: // 门店出样
			intent.setClass(mActivity, SvStoreSampleActivity.class);
			mActivity.startActivity(intent);
			break;
		case MENU_BOOTH: // 门店展台
			intent.setClass(mActivity, SvStoreBoothActivity.class);
			mActivity.startActivity(intent);
			break;
		case MENU_SALES:// 门店销量
			intent.setClass(mActivity, SvStoreSalesActivity.class);
			mActivity.startActivity(intent);
			break;
		case MENU_POSM: // POSM反馈
			intent.setClass(mActivity, SvStorePOSMActivity.class);
			mActivity.startActivity(intent);
			break;
		case MENU_COMPETE: // 竞品信息
			intent.setClass(mActivity, SvStoreCompeteActivity.class);
			mActivity.startActivity(intent);
			break;
		case MENU_PROMOTER: // 促销员信息
			intent.setClass(mActivity, SvStorePromoterInfoActivity.class);
			mActivity.startActivity(intent);
			break;
		case MENU_TASK: // 特殊任务
			intent.setClass(mActivity, SpecialTaskActivity.class);
			mActivity.startActivity(intent);
			break;
		}
		mActivity.finish();
	}
}
